﻿using System;
using System.Data;
using System.Windows.Forms;

namespace Stote
{
    public partial class FrmLogin : BaseForm
    {
        // 登录信息
        public static Dong.Model.User CurrentUser;
        public static Dong.Model.UserType CurrentUserType;
        Dong.BLL.User userBLL = new Dong.BLL.User();
        Dong.BLL.UserType userTypeBLL = new Dong.BLL.UserType();
        public FrmLogin()
        {
            InitializeComponent();
            btnLogin.Click += BtnLogin_Click;
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUsername.Text.Trim()))
            {
                MessageBox.Show("请输入账号！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                MessageBox.Show("请输入密码！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPassword.Focus();
                return;
            }

            // 拼接SQL,
            DataSet ds = userBLL.GetList($" Account='{txtUsername.Text.Trim()}' AND Password='{txtPassword.Text.Trim()}' AND Status=0");

            if (ds.Tables[0].Rows.Count <= 0)
            {
                MessageBox.Show("原因：\n1.账号或密码有误！\n2.账号被禁用！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
                return;
            }
            else if (ds.Tables[0].Rows.Count > 1)
            {
                MessageBox.Show("账号重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
                return;
            }

            CurrentUser = userBLL.GetModel(Convert.ToInt32(ds.Tables[0].Rows[0]["UserId"]));
            CurrentUserType = userTypeBLL.GetModel(CurrentUser.UserTypeId);

            DialogResult = DialogResult.OK;
        }
    }
}
